<template>
    <section class="section section-shaped section-lg my-0">
        <div class="shape shape-style-1 bg-gradient-default">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div v-if="showError" class="contentDiv">
            <base-alert type="danger">
                {{errorMsg}}
            </base-alert>
        </div>
        <div v-if="showInfo" class="contentDiv">
            <base-alert class="col-md-6" type="dark">
                {{infoMsg}}
            </base-alert>
        </div>
        <div class="container pt-lg-md">
            <div class="row justify-content-center">
                <div class="col-lg-12">
                    <card v-if="dataLoaded" type="secondary" shadow
                          header-classes="bg-white pb-5"
                          body-classes="px-lg-5 py-lg-5"
                          class="border-0">
                            <work-order-component v-for="(order,index) in orders" :key="index" :msg="order"></work-order-component>
                        </card>
                </div>
            </div>
        </div>
    </section>
</template>
<script>
    import {
        adminGetWorkOrder,
        checkCookie, getAllWorker,
        getFeedbackExtra,
        userGetWorkOrder,
        workerGetWorkOrder
    } from "../api";
import WorkOrderComponent from "./components/workOrderComponent";

export default {
    data() {
        return{
            role: 'user',
            paid:true,
            processing:true,
            processed:false,
            showError: false,
            showInfo: false,
            errorMsg: '',
            infoMsg: '',
            orders:[],
            results: [],
            workers: [],
            loadCount: 0,
            dataLoaded: false,
        }
    },
    methods:{
        message(msg){
            this.showError = true;
            this.errorMsg = msg;
        },
        infoMessage(msg){
            this.showInfo = true;
            this.infoMsg = msg;
        }
    },
    created() {
        checkCookie()
        .then(res => {
            if (res.code === "1"){
                this.role = res.data;
            }
        })
        .then(() => {
            if (this.role === 'user'){
                userGetWorkOrder()
                .then(res => {
                    if (res.code === "1"){
                        this.results = res.data;
                        this.loadCount = this.results.length;
                        this.results.forEach(item => {
                            item.oid = item.id;
                            item.role = this.role;
                            getFeedbackExtra({rid: item.id}).then(cres=>{
                                if (cres.code === "1"){
                                    this.dataLoaded = false;
                                    item.houseName = cres.data.houseName;
                                    item.workerName = cres.data.workerName;
                                    item.customerName = cres.data.customerName;
                                    this.loadCount--;
                                    if (this.loadCount === 0){
                                        this.dataLoaded = true
                                    }
                                } else {
                                    this.message(res.msg);
                                    return false
                                }
                            }).catch(err=>{
                                this.message(err);
                                return false;
                            });
                        });
                        // console.log("items", this.results);
                    } else {
                        this.message(res.msg);
                        return false
                    }
                }).then(()=>{
                    this.orders = this.results;
                    if (this.orders === [])
                    {
                        this.infoMessage("您还没有工单信息");
                    }
                    }).catch(err=>{
                    this.message(err);
                    return false;
                })
            } else if (this.role === 'worker'){
                workerGetWorkOrder()
                .then(res => {
                    if (res.code === "1"){
                        this.results = res.data;
                        this.loadCount = this.results.length;
                        this.results.forEach(item => {
                            item.oid = item.id;
                            item.role = this.role;
                            getFeedbackExtra({rid: item.id})
                            .then(cres=>{
                            if (cres.code === "1"){
                                this.dataLoaded = false;
                                item.houseName = cres.data.houseName;
                                item.workerName = cres.data.workerName;
                                item.customerName = cres.data.customerName;
                                this.loadCount--;
                                if (this.loadCount === 0){
                                    this.dataLoaded = true
                                }
                            } else {
                                this.message(res.msg);
                                return false
                            }
                            }).catch(err=>{
                                this.message(err);
                                return false;
                            });
                        })
                    } else {
                        this.message(res.msg);
                        return false
                    }
                }).then(()=>{
                    this.orders = this.results;
                    if (this.orders.length === 0)
                    {
                        this.infoMessage("您还没有工单信息");
                    }
                }).catch(err=>{
                    this.message(err);
                    return false;
                })
            } else {
                adminGetWorkOrder()
                .then(res => {
                    if (res.code === "1"){
                        this.results = res.data;
                        this.loadCount = this.results.length;
                        getAllWorker().then(wres=>{
                            if (wres.code === "1"){
                                this.workers = wres.data;
                                this.results.forEach(item => {
                                    item.oid = item.id;
                                    item.role = this.role;
                                    item.workers = this.workers;
                                    getFeedbackExtra({rid: item.id})
                                    .then(cres=>{
                                        if (cres.code === "1"){
                                            this.dataLoaded = false;
                                            item.houseName = cres.data.houseName;
                                            item.workerName = cres.data.workerName;
                                            item.customerName = cres.data.customerName;
                                            this.loadCount--;
                                            if (this.loadCount === 0){
                                                this.dataLoaded = true
                                            }
                                        } else {
                                            this.message(res.msg);
                                            return false
                                        }
                                        }).catch(err=>{
                                        this.message(err);
                                        return false;
                                    });
                                })
                            } else {
                                this.message(res.msg);
                                return false
                            }
                        }).then(()=>{
                            this.orders = this.results;
                            if (this.orders.length === 0)
                            {
                                this.infoMessage("您还没有工单信息");
                            }
                        }).catch(err=>{
                            this.message(err);
                            return false;
                        });
                    } else {
                        this.message(res.msg);
                        return false
                    }
                }).catch(err=>{
                    this.message(err);
                    return false;
                })
            }
        })
        .catch(error=>{
            this.message(error);
        });
    },
    components:{
        WorkOrderComponent
    }
};
</script>
<style>
.contentDiv{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    margin: auto;
}
</style>
